const urlimg = "http://zx.lihongnet.cn/" //公共路径
const commoneUrl = "http://zx.lihongnet.cn/api/" //公共路径

function httppost() {
	var promise = new Promise((resolve, reject) => {
		uni.request({
			url: 'https://mp.boxiyun.com/api/domain/getDomain',
			method: 'POST',
			data: {
				id: 9
			},
			success(res) {
				console.log(res.data.data.domain + '/api/')
				//return 
				resolve(res.data.data.domain + '/api/');
				uni.setStorageSync('httpip', res.data.data.domain + '/api/')
			}
		})
	})
	return promise;
}
//post请求封装
function postRequest(url, data, type) {
	var promise = new Promise((resolve, reject) => {
		// httppost().then(function(doadmin) {
		//return postRequestRNi(url, data, type, doadmin);
		resolve(postRequestRNi(url, data, type, 'http://zx.lihongnet.cn/api/'));
		// });

	})
	return promise;
}

function postRequestRNi(url, data, type, doadmin) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: doadmin + url,
			data: postData,
			method: "POST",
			// dataType:"jsonp",
			// jsonp:"callbackparam",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名
			// jsonpCallback:"success_jsonpCallback",

			header: {
				"content-type": type || "application/json;charset=UTF-8",
				token: uni.getStorageSync("token"),
			},
			success: function(res) {
				console.log(res);
				if (res.data.code == -1) {
					uni.showToast({
						icon: "none",
						title: res.data.message,
					});
					uni.clearStorage();
					setTimeout(function() {
						uni.reLaunch({
							url: "/pages/detection/detection",
						});
					}, 1000);
				} else if (res.data.code == 200) {} else {
					if (res.data.message == "") {} else {
						uni.showToast({
							icon: "none",
							title: res.data.message,
						});
					}
				}
				resolve(res.data);
			},
			error: function(e) {
				reject("网络出错");
			},
		});
	});
	return promise;
}

//get请求封装
function getRequest(url, data, type) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "GET",
			dataType: "json",
			header: {
				"content-type": type || "application/json",
				token: uni.getStorageSync("token"),
			},
			success: function(res) {
				resolve(res.data);
			},
			error: function(e) {
				reject("网络出错");
			},
		});
	});
	return promise;
}
//put请求封装
function putRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "PUT",
			dataType: "json",
			header: {
				"content-type": "application/json",
				token: uni.getStorageSync("token"),
			},
			success: function(res) {
				resolve(res.data);
			},
			error: function(e) {
				reject("网络出错");
			},
		});
	});
	return promise;
}
//del请求封装
function delRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "DELETE",
			dataType: "json",
			header: {
				"content-type": "application/json",
				token: uni.getStorageSync("token"),
			},
			success: function(res) {
				resolve(res.data);
			},
			error: function(e) {
				reject("网络出错");
			},
		});
	});
	return promise;
}

const wxLogin = function(openid) {
	return new Promise((resolve, reject) => {
		uni.login({
			success(res) {
				if (res.code) {
					resolve(res.code);
				} else {
					reject(res.errMsg);
				}
			},
		});
	});
};
/*微信小程序登录*/
const wechatAppLogin = function() {
	/*登录提示*/
	// loading("正在授权")
	uni.showLoading({
		title: "正在授权",
	});
	uni.getUserProfile({
		desc: "获取用户授权",
		success: (res) => {
			let userInfo = res.userInfo;
			wxLogin().then((code) => {
				// 引用uni.login()封装
				postRequest("api_get_openid", {
					code: code,
				}).then(
					(res) => {
						uni.hideLoading();
						// uni.setStorageSync("userinfo", userInfo);
						uni.setStorageSync("openid", res.openid);
						// uni.setStorageSync("session_key", res.session_key);
					},
					function(error) {
						reject(error);
					}
				);
			});
		},
	});
};

// 获取手机号授权
const getPhoneNumber = function(event) {
	let that = this;
	let code = event.detail; //获取手机code
	var promise = new Promise(function(resolve, reject) {
		uni.checkSession({
			success: (res) => {
				postRequest("api_get_phone", {
					code: code.code,
					openid: uni.getStorageSync("openid"),
				}).then(
					(res) => {
						if (res.code == -1) {
							uni.showToast({
								icon: "none",
								title: "登录失败",
							});
							return false;
						}
						uni.setStorageSync("user", res.user);
						uni.setStorageSync("phone", res.phone);
						uni.showToast({
							icon: "none",
							title: "登录成功",
						});
						setTimeout(function() {
							uni.switchTab({
								url: "/pages/index/user",
							});
						}, 1000);
					},
					function(error) {
						reject(error);
					}
				);
			},
			fail(err) {
				login();
			},
		});
	});
	return promise;
};

module.exports = {
	post: postRequest,
	get: getRequest,
	put: putRequest,
	del: delRequest,
	ip: commoneUrl,
	urlimg: urlimg,
	getPhoneNumber: getPhoneNumber,
	wechatAppLogin: wechatAppLogin,
};